package com.hitalkie.teacher.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hitalkie.teacher.R;
import com.hitalkie.teacher.model.User;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Date 09/12/2016
 *
 * @author charliema
 */

public class StudentRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private List<User> mData = new ArrayList<>();

    private OnItemClickListener mOnItemClick;

    public void setData(List<User> list) {
        if (list != null) {
            this.mData = list;
        }
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClick) {
        mOnItemClick = onItemClick;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder) holder).bindData(mData.get(holder.getAdapterPosition()));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * view holder
     */
    private class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivAvatar;
        TextView tvName;
        TextView tvEvaluation;

        ViewHolder(ViewGroup parent) {
            super(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_student, parent, false));

            ivAvatar = (ImageView) itemView.findViewById(R.id.ivAvatar);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            tvEvaluation = (TextView) itemView.findViewById(R.id.tvEvaluation);
        }

        void bindData(final User data) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClick != null) {
                        mOnItemClick.onItemClick(data);
                    }
                }
            });
            if (data.avatar != null) {
                Picasso.with(ivAvatar.getContext()).load(data.avatar)
                        .placeholder(R.drawable.ic_default_avatar)
                        .error(R.drawable.ic_default_avatar)
                        .fit().centerCrop().into(ivAvatar);
            }
            tvName.setText(data.name);

            if (data.is_evaluated == 0) {
                tvEvaluation.setVisibility(View.VISIBLE);
                tvEvaluation.setEnabled(true);
                tvEvaluation.setText("写评价");
                tvEvaluation.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mOnItemClick != null) {
                            mOnItemClick.onEvaluation(data);
                        }
                    }
                });
            } else if (data.is_evaluated == 1) {
                tvEvaluation.setVisibility(View.VISIBLE);
                tvEvaluation.setEnabled(false);
                tvEvaluation.setText("已评价");
            } else {
                tvEvaluation.setVisibility(View.GONE);
            }


        }
    }

    /**
     * click event
     */
    public interface OnItemClickListener {
        void onItemClick(User data);

        void onEvaluation(User data);
    }

}